<!-- html -->
<template>
  <h2 class="h2">toRefs，复制对象的所有属性并转成ref对象</h2>
  <span class="span2">num1：</span>
  <input class="input1" type="number" v-model="nums.num1">
  <span class="span2">num2：</span>
  <input class="input1" type="number" v-model="nums.num2">
  <button class="button1" @click="changeNum1">修改num1</button>
  <button class="button1" @click="showSum">查看和</button>
  <hr class="hr1">
  <li class="li1" v-for="item in numsArray" :key="item.id">num1：{{ item.num1 }}，num2：{{ item.num2 }}</li>
  <button class="button1" @click="changeFirstNum1">修改第一个num1</button>
  <button class="button1" @click="showFirstSum">查看第一个和</button>
</template>

<!-- js或ts -->
<script lang="ts">
export default {
  name: 'toRefsTest1'
}
</script>

<!-- js或ts -->
<script setup lang="ts">
import { reactive, toRefs } from 'vue'
//数据
let nums = reactive({ num1: 1, num2: 1 })
let numsArray = reactive([
  { id: 1, num1: 1, num2: 1 },
  { id: 2, num1: 1, num2: 1 }
])
let numsRef = toRefs(nums)
let numsArrayRef = toRefs(numsArray)
console.log('numsRef：', numsRef)
console.log('numsArrayRef：', numsArrayRef)
//方法
function changeNum1() {
  numsRef.num1.value += 1
  console.log('num.num1', nums.num1)
  console.log('numsRef.num1：', numsRef.num1.value)
}
function showSum() {
  alert(nums.num1 - 0 + nums.num2)
}
function changeFirstNum1() {
  numsArrayRef[0].value.num1 += 1
  console.log('numsArray[0].num1：', numsArray[0].num1)
  console.log('numsArrayRef[0].num1：', numsArrayRef[0].value.num1)
}
function showFirstSum() {
  alert(numsArray[0].num1 - 0 + numsArray[0].num2)
}
</script>

<!-- 样式 -->
<style scoped></style>